package controlador;



import modelo.ErrorSintaxis;
import modelo.Expresion;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import vista.Pantalla;
import vista.Validator;

public class ControladorBotones implements ActionListener
{

    private Pantalla input;
    public double ultimoValor;

    public ControladorBotones(Pantalla input)
    {
        this.input = input;
        this.ultimoValor = 0;
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        int caretPosition = input.getCaretPosition();
        if (e.getSource() instanceof JButton)
        {
            JButton button = (JButton) e.getSource();
            if (button.getText().equals("CE"))
            {
                input.quitarTexto(0, input.getText().length());
            } else if (button.getText().equals("->"))
            {
                if (caretPosition > 0)
                    input.quitarTexto(caretPosition - 1, 1);
            } else if (button.getText().equals("√x"))
            {
                input.anadirTexto("√(");
            } else if(button.getText().equals("exp"))
            {
                input.anadirTexto("e");
            } else if (button.getText().equals("<"))
            {
                if(caretPosition > 0)
                input.setCaretPosition(caretPosition-1);
            } else if (button.getText().equals(">"))
            {
                if(caretPosition< input.getText().length())
                input.setCaretPosition(caretPosition+1);
            } else if (button.getText().equals("="))
            {

                String expresion = input.getText();
                expresion = expresion.replace("√", "sqrt");
                expresion = expresion.replace("π", Double.toString(Math.PI));
                expresion = expresion.replace("sin", "Sin");
                expresion = expresion.replace("Ans", Double.toString(this.ultimoValor));

                try
                {
                    Validator.validate(expresion);
                    Expresion manejador = new Expresion(expresion);
                    this.ultimoValor = manejador.evaluarExpresion();
                    input.setText(Double.toString(this.ultimoValor));
                } catch (ErrorSintaxis ex)
                {
                    if (ex.getMessage().equalsIgnoreCase("infinity"))
                        {
                            input.setText(ex.getMessage());
                        } else
                        {
                            JOptionPane.showMessageDialog(null, ex.getMessage(), "Oops", JOptionPane.INFORMATION_MESSAGE);
                            input.setText("");
                        }
                }

            } else if (button.getText().equals("Log"))
            {
                input.anadirTexto("log(");
            } else if (button.getText().equals("n!"))
            {
                input.anadirTexto("fact(");
            } else
            {
                input.anadirTexto(button.getText());
            }
        }
    }
}